package com.molihub.service.impl;

import com.molihub.entity.*;
import com.molihub.service.ArticleService;
import com.molihub.utils.LogUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: ArticleServiceImpl
 * @Description: 文章业务层接口实现类
 * @author: xw，GGBOY
 * @date 2020/1/26 21:12
 * @Version: 1.0
 **/
@Service
public class ArticleServiceImpl extends BaseService implements ArticleService {

    Logger logger = LogUtils.getInstance(UserServiceImpl.class);

    @Override
    public Integer publishArticle(Article article) {
        article = dealWithKindsAndTags(article);
        return articleMapper.addArticle(article);
    }


    @Override
    public Integer deleteArticle(Integer articleId) {
        return articleMapper.deleteArticleById(articleId);
    }

    @Override
    public List<Article> searchArticles(Article article) {
        return articleMapper.getArticles(article);
    }

    @Override
    public Article getArticleById(Integer articleId) {
        return articleMapper.getArticleById(articleId);
    }

    @Override
    public List<Article> getArticlesByUserIdAndDate(Integer userId, Date from, Date to) {
        return articleMapper.getArticlesByUserIdAndDate(userId, from, to);
    }

    @Override
    public Integer updateArticle(Article article) {
        article = dealWithKindsAndTags(article);
        return articleMapper.updateArticle(article);
    }

    @Override
    public ArticleCountStatistics statistics(Integer userId) {
        return articleMapper.statistics(userId);
    }

    @Override
    public List<Article> getArticlesByUserId(Integer userId) {
        return articleMapper.getArticlesByUserId(userId);
    }

    @Override
    public List<Article> getAllArticlesByUserId(Integer userId) {
        return articleMapper.getAllArticlesByUserId(userId);
    }

    @Override
    public List<Article> getMyArticles(Integer userId, String title) {
        return articleMapper.getMyArticles(userId, title);
    }

    @Override
    public Integer getLikeCountByArticle(Integer articleId) {
        return articleLikeMapper.getArticleTotalLikeCount(articleId);
    }

    @Override
    public Integer getCommentCountByArticle(Integer articleId) {
        return articleCommentMapper.getArticleTotalCommentCount(articleId);
    }

    @Override
    public List<Article> getArticlesByUserIdAndStatus(Integer userId, Integer articleStatus) {
        return articleMapper.getArticlesByUserIdAndStatus(userId, articleStatus);
    }

    @Override
    public Integer getArticleCountByUserIdAndKind(Integer userId, String kind) {
        return articleMapper.getArticleCountByUserIdAndKind(userId, kind);
    }

    @Override
    public Integer getArticleCountByUserIdAndTag(Integer userId, String tag) {
        return articleMapper.getArticleCountByUserIdAndTag(userId, tag);
    }

    @Override
    public Integer deleteArticleKind(Integer userId, String kindName) {
        return articleMapper.deleteArticleKind(userId, kindName);
    }

    @Override
    public Integer deleteArticleTag(Integer userId, String tagName) {
        return articleMapper.deleteArticleTag(userId, tagName);
    }

    @Override
    public Integer updateArticleKind(Integer userId, String newName, String oldName) {
        return articleMapper.updateArticleKind(userId, newName, oldName);
    }

    @Override
    public Integer updateArticleTag(Integer userId, String newName, String oldName) {
        return articleMapper.updateArticleTag(userId, newName, oldName);
    }

    @Override
    public Integer addReadCount(Integer articleId) {
        return articleMapper.addReadCount(articleId);
    }

    @Override
    public List<Article> getNewestFiveArticle() {
        return articleMapper.getNewestFiveArticle();
    }

    @Override
    public List<Article> getNewestFivePost() {
        return articleMapper.getNewestFivePost();
    }

    @Override
    public Integer getArticlesNumber() {
        return articleMapper.getArticlesNumber();
    }

    @Override
    public List<Article> getArticlesByUserAndTag(Integer id, String tag, Integer status, boolean secret,Integer aid) {
        return articleMapper.getArticlesByUserAndTag(id,tag,status,secret,aid);
    }

    @Override
    public List<Article> getFamousArticles(int userId) {
        return articleMapper.getFamousArticles(userId);
    }

    @Override
    public List<ArticleDateArchive> getArchive(Integer userId) {
        return articleMapper.getArchive(userId);
    }

    public Article dealWithKindsAndTags(Article article) {
        //处理标签
        if (article.getArticleTags() != null) {
            String tags = article.getArticleTags();
            String[] splitTags = tags.split(",");
            List<ArticleTag> tagsByUserId = articleTagMapper.getArticleTagsByUserId(article.getUserId());
            List<String> myTags = new ArrayList<>();
            for (int i = 0; i < tagsByUserId.size(); i++) {
                myTags.add(tagsByUserId.get(i).getArticleTagName());
            }
            for (String str : splitTags) {
                if (!myTags.contains(str)) {
                    articleTagMapper.addArticleTag(ArticleTag.builder().articleTagName(str).userId(article.getUserId()).build());
                }
            }
        }
        //处理类别
        if (article.getArticleKinds() != null) {
            String kinds = article.getArticleKinds();
            String[] splitkinds = kinds.split(",");
            List<ArticleKind> kindsByUserId = articleKindMapper.getArticleKindsByUserId(article.getUserId());
            List<String> myKinds = new ArrayList<>();
            for (int i = 0; i < kindsByUserId.size(); i++) {
                myKinds.add(kindsByUserId.get(i).getArticleKindName());
            }
            for (String str : splitkinds) {
                if (!myKinds.contains(str)) {
                    articleKindMapper.addArticleKind(ArticleKind.builder().articleKindName(str).userId(article.getUserId()).build());
                }
            }
        }
        return article;
    }

}
